<template>
	<view class="page">
		<!-- 搜索 -->
		<view class="search-head">
			<view class="back" @click="onBack">
				<text></text>
			</view>
			<view class="search">
				<text class="iconfont icon-fadajing"></text>
				<input type="text" v-model="keyword" placeholder="搜索商品" @confirm="fetchData" />
			</view>
			<view class="cut" @click="isList = !isList">
				<text class="iconfont" :class="isList?'icon-shitu01':'icon-shitu02'"></text>
			</view>
		</view>
		<!-- 筛选 -->
		<view class="screen-info">
			<view class="screen-list">
				<!-- <view class="list" :class="{'action':screenShow===0}" @click="onSynthesize(0)"> -->
				<view class="list" :class="{'action':sort_type===0}" @tap="onSort(0)">
					<text>综合</text>
					<text class="iconfont icon-sanjiao icon_z"></text>
				</view>
				<view class="list" :class="{'action':sort_type===6}" @tap="onSort(6)">
					<text>销量</text>
					<text class="iconfont icon-sanjiao icon_z"></text>
				</view>
				<view class="list" @tap="onPriceSort">
					<text  :class="{'action':sort_type===9 || sort_type===10 }">价格</text>
					<view class="icon_j">
						<text class="iconfont icon-sanjiao up" :class="{'action':sort_type===9}" ></text>
						<text class="iconfont icon-sanjiao down" :class="{'action':sort_type===10}"></text>
					</view>
				</view>
				<!-- <view class="list" @click="isDrawer = true">
					<text>筛选</text>
					<text class="iconfont icon-shaixuan icon_s"></text>
				</view> -->
			</view>
			<!-- 筛选弹窗 -->
			<view class="screen-popup" @click.stop="isScreen = false" v-show="isScreen">
				<view class="synthesize">
					<view class="list">
						<text class="check"></text>
						<text class="title">综合排序</text>
					</view>
					<view class="list">
						<text class="check"></text>
						<text class="title">评论数从高到低</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 商品列表 -->
		<view class="goods-data">
			<mescroll-body ref="mescrollRef"
				@init="mescrollInit"
				@down="downCallback"
				@up="upCallback"
				:down="downOption"
				:up="upOption"
				:top="0">
				<view class="goods-list">
					<view :class="isList?'list-view':'list-li'" v-for="(item,index) in goodsList" @click="onGoodsList(item)" :key="index">
						<view class="thumb">
							<image :src="item.goods_thumbnail_url" mode="heightFix"></image>
						</view>
						<view class="item">
							<view class="title">
								<text class="two-omit">{{item.goods_name}}</text>
							</view>
							<view class="price">
								<view class="retail-price">
									<!-- <text class="min"></text> -->
									<text class="max">券后￥{{(item.min_group_price - item.coupon_discount) / 100}}</text>
									<text class="sales-tip">已拼{{item.sales_tip}}件</text>
									<view class="tag" v-if="item.activity_type === 1">
										<text>秒杀</text>
									</view>
								</view>
								<view class="vip-price">
									<text class="min">优惠券￥</text>
									<text class="max">{{item.coupon_discount / 100}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</mescroll-body>
		</view>
		<!-- 抽屉 -->
		<view class="cu-modal drawer-modal justify-end" :class="{'show':isDrawer}" @click="isDrawer = false" style="position: absolute;">
			<view class="cu-dialog basis-lg" @click.stop="isDrawer = true">
				<view class="serve">
					<view class="title">
						<text>服务</text>
					</view>
					<view class="serve-list">
						<view class="list action">
							<text>会员专享</text>
						</view>
						<view class="list">
							<text>活动</text>
						</view>
						<view class="list">
							<text>活动</text>
						</view>
					</view>
				</view>
				<view class="price-screen">
					<view class="title">
						<text>价格区间</text>
					</view>
					<view class="price-section">
						<input type="number" placeholder="最低价">
						<text></text>
						<input type="number" placeholder="最高价">
					</view>
				</view>
				<view class="operation-btn">
					<view class="btn" @click.stop="isDrawer = false">
						<text>取消</text>
					</view>
					<view class="btn action">
						<text>确认</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import mixin from '@/mixins/app.js'
	export default {
		mixins: [MescrollMixin, mixin], // 使用mixin
		data() {
			return {
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					offset:200
				},
				// 列表视图切换
				isList: true,
				// 筛选弹窗
				isScreen: false,
				// 筛选切换
				screenShow: 0,
				// 抽屉
				isDrawer: false,
				keyword: '',
				goodsList:[],
				list_id:'',
				sort_type:0,
				page:0
			}
		},
		onLoad(params) {
			this.keyword = decodeURIComponent(params.keyword||'');
			if (this.keyword) {
				// this.fetctAuthority()
			}
		},
		methods: {
			onSort(val) {
					this.sort_type = val
					this.fetchData()
			},
			onPriceSort() {
					let sort_type = 9
					if (this.sort_type === 9) {
						sort_type = 10
					} else if (this.sort_type === 10) {
						sort_type = 9
					}
					
					this.sort_type = sort_type 
					this.fetchData()
			},
			fetctAuthority() {
				
				this.checkAuthority(this.pdd_pid, this.fetchData())
				
			},
			fetchData() {
				this.$cloud('pdd', {
					type: 'pdd.ddk.goods.search',
					keyword:this.keyword,
					with_coupon: true,
					pid:this.pdd_pid,
					sort_type: this.sort_type,
					page:1,
					page_size:20
				}).then(res => {

					this.goodsList = res.goods_search_response.goods_list
					this.list_id = res.list_id
					this.mescroll.endByPage(20,100);
				})
			},
			/*下拉刷新的回调, 有三种处理方式:*/
			downCallback(){

				// this.fetchData()
				this.mescroll.endSuccess();
			},
			/*上拉加载的回调*/
			upCallback(page) {

				this.page += 1
				
				if (this.page === 1) {
					this.fetchData()
				} else {
					this.$cloud('pdd', {
						type: 'pdd.ddk.goods.search',
						keyword:this.keyword,
						with_coupon: true,
						pid:this.pdd_pid,
						list_id: this.list_id,
						page:this.page,
						page_size:20
					}).then(res => {

						this.goodsList =  this.goodsList.concat(res.goods_search_response.goods_list) 
						this.mescroll.endByPage(20,100);
					})
				}
				
				
				// setTimeout(() =>{
				// 	this.mescroll.endByPage(10, 20);
				// },2000)
			},
			/**
			 * 返回点击
			 */
			onBack(){
				uni.navigateBack();
			},
			/**
			 * 综合点击
			 * @param {Number} type
			 */
			onSynthesize(type){
				this.screenShow = type;
				this.isScreen = !this.isScreen;
			},
			/**
			 * 商品列表点击
			 */
			onGoodsList(item){
				let url = `/pages/GoodsDetails/GoodsDetails?goods_sign=${item.goods_sign}&goods_id=${item.goods_id}&search_id=${item.search_id}`
				uni.navigateTo({
					url: url,
					animationType: 'zoom-fade-out',
					animationDuration: 200
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import  'SearchGoodsList.scss';
</style>
